﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeoSketch2.Core;
using System.Drawing;

namespace GeoSketch2.Archaeo
{
    /// <summary>
    /// A tool to construct a point using 2 distances from 2 known points
    /// A third click is needed to choose which intersection is the right one
    /// </summary>
    public class TCirclesIntersection: BaseArchaeoTool
    {
        Point2D Point1, Point2;
        double Radius1, Radius2;

        #region Constructor
        /// <summary>
        /// A tool to construct a point using 2 distances from 2 known points
        /// A third click is needed to choose which intersection is the right one
        /// </summary>
        public TCirclesIntersection(Viewport Viewport)
            : base(Viewport,"Circle intersection")
        {
        } 
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            ExternalPointSent(Viewport.Object2ImageTransformation.InverseTransform(MouseArgs.Location));
            RefreshViewport();
        }

        /// <summary>
        /// The method called when a point in object coordinates is sent to the tool
        /// </summary>
        public override void ExternalPointSent(Point2D64D Point)
        {
            if (Point1 == null)
            {
                Point1 = new Point2D(Point, null, null);
                SendMessage("Enter the distance to the first point");
                ShowTextInput();
            }
            else if (Point2 == null)
            {
                Point2 = new Point2D(Point, null, null);
                SendMessage("Enter the distance to the second point");
                ShowTextInput();
            }
            else
            {
                Point2D NewPoint = new Point2D(
                GeoMath.CirclesIntersection(Point1.Position, Radius1, Point2.Position, Radius2, Point),
                Viewport.CurrentLayer,
                null,
                Viewport.CurrentColor,
                Viewport.CurrentLineType);
            Viewport.GeoObjects.Add(NewPoint);
            NewPoint.Draw(Viewport);
                Point1 = null;
                Radius1 = 0;
                Point2 = null;
                Radius2 = 0;
                SendMessage("Click the first point");
            }
        }

        /// <summary>
        /// The method called when a text is sent to the tool
        /// </summary>
        public override void TextEntry(string Text)
        {
            if (Point1 == null && Radius1 == 0)
            {
                ExternalPointSent(GetCoordinatesFromString(Text));
            }
            else if (Point1!=null&&Radius1==0)
            {
                Radius1 = Convert.ToDouble(Text.Replace(',', '.'), Core.Vars.Setup.GeoSketch.NumberFormat);
                SendMessage("Click the second point");
            }
            else if (Point2 == null && Radius2 == 0)
            {
                ExternalPointSent(GetCoordinatesFromString(Text));
            }
            else if (Point2 != null && Radius2 == 0)
            {
                Radius2 = Convert.ToDouble(Text.Replace(',', '.'), Core.Vars.Setup.GeoSketch.NumberFormat);
                SendMessage("Click the intersection wanted");
            }
            else
            {
                ExternalPointSent(GetCoordinatesFromString(Text));
            }
            RefreshViewport();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Draw the graphics requested by the object
        /// </summary>
        public override void Draw()
        {
            if (Point1 != null)
            {
                Point1.DrawCustomPen(Viewport, Vars.Setup.Tools.TCirclesIntersection.CircleCenterPen);

                if (Radius1 > 0)
                {
                    Viewport.Graphic.DrawEllipse(Vars.Setup.Tools.TCirclesIntersection.CirclePen,
                        RectangleD.FromCenter(Viewport.Object2ImageTransformation.Transform(Point1.Position),
                        Viewport.Object2ImageTransformation.Transform(2 * Radius1),
                        Viewport.Object2ImageTransformation.Transform(2 * Radius1),
                        YAxisDirection.DownSide));
                }
            }
            if (Point2 != null)
            {
                Point2.DrawCustomPen(Viewport, Vars.Setup.Tools.TCirclesIntersection.CircleCenterPen);

                if (Radius2 > 0)
                {
                    Viewport.Graphic.DrawEllipse(Vars.Setup.Tools.TCirclesIntersection.CirclePen,
                        RectangleD.FromCenter(Viewport.Object2ImageTransformation.Transform(Point2.Position),
                        Viewport.Object2ImageTransformation.Transform(2 * Radius2),
                        Viewport.Object2ImageTransformation.Transform(2 * Radius2),
                        YAxisDirection.DownSide));
                }
            }
        }

        /// <summary>
        /// The tool can call this method to display a message to the user
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Click the first point");
        } 
        #endregion
    }
}
